﻿<!DOCTYPE html>
<!--[if IE]><![endif]-->
<html>
  
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Utilities </title>
    <meta name="viewport" content="width=device-width">
    <meta name="title" content="Utilities ">
    <meta name="generator" content="docfx 2.51.0.0">
    
    <link rel="shortcut icon" href="../favicon.ico">
    <link rel="stylesheet" href="../styles/docfx.vendor.css">
    <link rel="stylesheet" href="../styles/docfx.css">
    <link rel="stylesheet" href="../styles/main.css">
    <meta property="docfx:navrel" content="../toc.html">
    <meta property="docfx:tocrel" content="toc.html">
    
    
    
  </head>
  <body data-spy="scroll" data-target="#affix" data-offset="120">
    <div id="wrapper">
      <header>
        
        <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
          <div class="container">
            <div class="navbar-header">
              <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
              
              <a class="navbar-brand" href="../index.html">
                <img id="logo" class="svg" src="../logo.svg" alt="">
              </a>
            </div>
            <div class="collapse navbar-collapse" id="navbar">
              <form class="navbar-form navbar-right" role="search" id="search">
                <div class="form-group">
                  <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
                </div>
              </form>
            </div>
          </div>
        </nav>
        
        <div class="subnav navbar navbar-default">
          <div class="container hide-when-search" id="breadcrumb">
            <ul class="breadcrumb">
              <li></li>
            </ul>
          </div>
        </div>
      </header>
      <div role="main" class="container body-content hide-when-search">
        
        <div class="sidenav hide-when-search">
          <a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a>
          <div class="sidetoggle collapse" id="sidetoggle">
            <div id="sidetoc"></div>
          </div>
        </div>
        <div class="article row grid-right">
          <div class="col-md-10">
            <article class="content wrap" id="_content" data-uid="">
<h1 id="utilities">Utilities</h1>

<h3 id="accesstools">AccessTools</h3>
<p>To simplify reflections, Harmony has a helper class called AccessTools. Here are the most commonly used methods:</p>
<pre><code class="lang-csharp">public static BindingFlags all = ....
public static Type TypeByName(string name)
public static FieldInfo Field(Type type, string name)
public static PropertyInfo Property(Type type, string name)
public static MethodInfo Method(Type type, string name, Type[] parameters = null, Type[] generics = null)
public static ConstructorInfo Constructor(Type type, Type[] parameters = null)
public static Type Inner(Type type, string name)
public static Type FirstInner(Type type, Func&lt;Type, bool&gt; predicate)
</code></pre>
<p>Any of these methods use the <strong>all</strong> BindingFlags definition and thus work on anything regardless if it is public, private, static or else.</p>
<h3 id="traverse">Traverse</h3>
<p>In order to access fields, properties and methods from classes via reflection, Harmony contains a utility called Traverse. Think of it as LINQ for classes. Here are the main methods:</p>
<pre><code class="lang-csharp">// starting from a type or instance
public static Traverse Create(Type type)
public static Traverse Create&lt;T&gt;()
public static Traverse CreateWithType(string name)

// digging deeper
public Traverse Type(string name)
public Traverse Field(string name)
public Traverse Property(string name, object[] index = null)
public Traverse Method(string name, params object[] arguments)
public Traverse Method(string name, Type[] paramTypes, object[] arguments = null)

// calling getter or method
public object GetValue()
public T GetValue&lt;T&gt;()
public object GetValue(params object[] arguments)
public T GetValue&lt;T&gt;(params object[] arguments)
public override string ToString()

// calling setter
public Traverse SetValue(object value)

// iterating
public static void IterateFields(object source, Action&lt;Traverse&gt; action)
public static void IterateFields(object source, object target, Action&lt;Traverse, Traverse&gt; action)
public static void IterateProperties(object source, Action&lt;Traverse&gt; action)
public static void IterateProperties(object source, object target, Action&lt;Traverse, Traverse&gt; action)
</code></pre>
<p>Example:</p>
<pre><code class="lang-csharp" name="example">class Foo
{
    struct Bar
    {
        static string secret = &quot;hello&quot;;

        public string ModifiedSecret()
        {
            return secret.ToUpper();
        }
    }

    Bar myBar
    {
        get
        {
            return new Bar();
        }
    }

    public string GetSecret()
    {
        return myBar.ModifiedSecret();
    }

    Foo()
    {
    }

    static Foo MakeFoo()
    {
        return new Foo();
    }
}

void Test()
{
    var foo = Traverse.Create&lt;Foo&gt;().Method(&quot;MakeFoo&quot;).GetValue&lt;Foo&gt;();
    Traverse.Create(foo).Property(&quot;myBar&quot;).Field(&quot;secret&quot;).SetValue(&quot;world&quot;);
    Console.WriteLine(foo.GetSecret()); // outputs WORLD
}
</code></pre>
<p>Although most fields, properties and methods in that class hierarchy are private, Traverse can easily access anything. It has build-in null protection and propagates null as a result if any of the intermediates would encounter null. It works with static types and caches lookups which makes it pretty fast.</p>
<h3 id="filelog">FileLog</h3>
<p>For simple and quick logging, Harmony uses a tool class FileLog. It has three methods:</p>
<pre><code class="lang-csharp">public static void Log(string str)
// Creates a new log file called &quot;harmony.log.txt&quot; on the computers Desktop (if it not already exists) and appends *str* to it. 

public static void Reset()
// Deletes the log file.

public static unsafe void LogBytes(long ptr, int len)
// Same as Log(string str) but logs a hex dump and md5 hash.
</code></pre>
</article>
          </div>
          
          <div class="hidden-sm col-md-2" role="complementary">
            <div class="sideaffix">
              <div class="contribution">
                <ul class="nav">
                  <li>
                    <a href="https://github.com/pardeike/Harmony/blob/master/Harmony/Documentation/articles/utilities.md/#L1" class="contribution-link">Improve this Doc</a>
                  </li>
                </ul>
              </div>
              <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
              <!-- <p><a class="back-to-top" href="#top">Back to top</a><p> -->
              </nav>
            </div>
          </div>
        </div>
      </div>
      
      <footer>
        <div class="grad-bottom"></div>
        <div class="footer">
          <div class="container">
            <span class="pull-right">
              <a href="#top">Back to top</a>
            </span>
            
            <span>Generated by <strong>DocFX</strong></span>
          </div>
        </div>
      </footer>
    </div>
    
    <script type="text/javascript" src="../styles/docfx.vendor.js"></script>
    <script type="text/javascript" src="../styles/docfx.js"></script>
    <script type="text/javascript" src="../styles/main.js"></script>
  </body>
</html>
